package c.c.a;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import com.huawei.hms.analytics.core.crypto.AesCipher.AesLen;
import java.util.ArrayList;
import org.json.JSONException;
import org.json.JSONObject;

public class c {
    public static final String[] h = new String[]{"channel", "package", "app_version"};
    public boolean a;
    public final Context b;
    public final g2 c;
    public JSONObject d;
    public final ArrayList<b2> e = new ArrayList(32);
    public final SharedPreferences f;
    public int g = 0;

    public c(Context context, g2 g2Var) {
        this.b = context;
        this.c = g2Var;
        this.f = g2Var.e;
        this.d = new JSONObject();
        ((c1) t0.b.b(context)).a();
    }

    public static void b(JSONObject jSONObject, String str, String str2) {
        if (!TextUtils.isEmpty(str2)) {
            jSONObject.put(str, str2);
        }
    }

    public static boolean g(String str) {
        int length = str != null ? str.length() : 0;
        if (length < 13 || length > AesLen.ROOTKEY_COMPONET_LEN) {
            return false;
        }
        for (int i = 0; i < length; i++) {
            char charAt = str.charAt(i);
            if ((charAt < '0' || charAt > '9') && ((charAt < 'a' || charAt > 'f') && ((charAt < 'A' || charAt > 'F') && charAt != '-'))) {
                return false;
            }
        }
        return true;
    }

    public static boolean i(String str) {
        int i = 0;
        if (TextUtils.isEmpty(str) || "unknown".equalsIgnoreCase(str) || "Null".equalsIgnoreCase(str)) {
            return false;
        }
        for (int i2 = 0; i2 < str.length(); i2++) {
            if (str.charAt(i2) != '0') {
                break;
            }
        }
        i = 1;
        return i ^ 1;
    }

    public JSONObject a() {
        return this.a ? this.d : null;
    }

    public JSONObject c() {
        String str = "oaid";
        JSONObject a = a();
        if (a != null) {
            try {
                JSONObject optJSONObject = a.optJSONObject(str);
                String str2 = t0.a;
                CharSequence charSequence = null;
                if (optJSONObject != null) {
                    charSequence = optJSONObject.optString("id", null);
                }
                if (!TextUtils.isEmpty(charSequence)) {
                    optJSONObject = new JSONObject();
                    n0.d(optJSONObject, a);
                    optJSONObject.put(str, charSequence);
                    return optJSONObject;
                }
            } catch (Exception e) {
                m0.b(e);
            }
        }
        return a;
    }

    public final boolean d(String str, Object obj) {
        boolean z;
        Object opt = this.d.opt(str);
        if ((obj == null || obj.equals(opt)) && (obj != null || opt == null)) {
            z = false;
        } else {
            synchronized (this) {
                try {
                    JSONObject jSONObject = this.d;
                    JSONObject jSONObject2 = new JSONObject();
                    n0.d(jSONObject2, jSONObject);
                    jSONObject2.put(str, obj);
                    this.d = jSONObject2;
                } catch (JSONException e) {
                    m0.b(e);
                }
            }
            z = true;
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("updateHeader, ");
        stringBuilder.append(str);
        stringBuilder.append(", ");
        stringBuilder.append(opt);
        stringBuilder.append(", ");
        stringBuilder.append(obj);
        m0.a(stringBuilder.toString(), null);
        return z;
    }

    public int e() {
        String str = "version_code";
        int optInt = this.a ? this.d.optInt(str, -1) : -1;
        for (int i = 0; i < 3 && optInt == -1; i++) {
            h();
            optInt = this.a ? this.d.optInt(str, -1) : -1;
        }
        return optInt;
    }

    public String f() {
        String str = "app_version";
        String optString = this.a ? this.d.optString(str, null) : null;
        for (int i = 0; i < 3 && optString == null; i++) {
            h();
            optString = this.a ? this.d.optString(str, null) : null;
        }
        return optString;
    }

    /* JADX WARNING: Missing block: B:23:0x013b, code skipped:
            if (r10 != false) goto L_0x013d;
     */
    public boolean h() {
        /*
        r13 = this;
        r0 = r13.e;
        monitor-enter(r0);
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r1 = r1.size();	 Catch:{ all -> 0x0241 }
        if (r1 != 0) goto L_0x00df;
    L_0x000b:
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r2 = new c.c.a.c2;	 Catch:{ all -> 0x0241 }
        r2.m5222init();	 Catch:{ all -> 0x0241 }
        r1.add(r2);	 Catch:{ all -> 0x0241 }
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r2 = new c.c.a.e2;	 Catch:{ all -> 0x0241 }
        r3 = r13.b;	 Catch:{ all -> 0x0241 }
        r2.m5226init(r3);	 Catch:{ all -> 0x0241 }
        r1.add(r2);	 Catch:{ all -> 0x0241 }
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r2 = new c.c.a.f2;	 Catch:{ all -> 0x0241 }
        r3 = r13.b;	 Catch:{ all -> 0x0241 }
        r4 = r13.c;	 Catch:{ all -> 0x0241 }
        r2.m5228init(r3, r4);	 Catch:{ all -> 0x0241 }
        r1.add(r2);	 Catch:{ all -> 0x0241 }
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r2 = new c.c.a.d;	 Catch:{ all -> 0x0241 }
        r3 = r13.b;	 Catch:{ all -> 0x0241 }
        r2.m5225init(r3);	 Catch:{ all -> 0x0241 }
        r1.add(r2);	 Catch:{ all -> 0x0241 }
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r2 = new c.c.a.f;	 Catch:{ all -> 0x0241 }
        r3 = r13.b;	 Catch:{ all -> 0x0241 }
        r2.m5229init(r3);	 Catch:{ all -> 0x0241 }
        r1.add(r2);	 Catch:{ all -> 0x0241 }
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r2 = new c.c.a.g;	 Catch:{ all -> 0x0241 }
        r3 = r13.b;	 Catch:{ all -> 0x0241 }
        r4 = r13.c;	 Catch:{ all -> 0x0241 }
        r2.m5233init(r3, r4);	 Catch:{ all -> 0x0241 }
        r1.add(r2);	 Catch:{ all -> 0x0241 }
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r2 = new c.c.a.h;	 Catch:{ all -> 0x0241 }
        r3 = r13.b;	 Catch:{ all -> 0x0241 }
        r2.m5238init(r3);	 Catch:{ all -> 0x0241 }
        r1.add(r2);	 Catch:{ all -> 0x0241 }
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r2 = new c.c.a.j;	 Catch:{ all -> 0x0241 }
        r3 = r13.b;	 Catch:{ all -> 0x0241 }
        r2.m5244init(r3);	 Catch:{ all -> 0x0241 }
        r1.add(r2);	 Catch:{ all -> 0x0241 }
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r2 = new c.c.a.k;	 Catch:{ all -> 0x0241 }
        r3 = r13.b;	 Catch:{ all -> 0x0241 }
        r4 = r13.c;	 Catch:{ all -> 0x0241 }
        r2.m5245init(r3, r4);	 Catch:{ all -> 0x0241 }
        r1.add(r2);	 Catch:{ all -> 0x0241 }
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r2 = new c.c.a.l;	 Catch:{ all -> 0x0241 }
        r2.m5247init();	 Catch:{ all -> 0x0241 }
        r1.add(r2);	 Catch:{ all -> 0x0241 }
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r2 = new c.c.a.m;	 Catch:{ all -> 0x0241 }
        r3 = r13.c;	 Catch:{ all -> 0x0241 }
        r2.m5249init(r3);	 Catch:{ all -> 0x0241 }
        r1.add(r2);	 Catch:{ all -> 0x0241 }
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r2 = new c.c.a.n;	 Catch:{ all -> 0x0241 }
        r3 = r13.b;	 Catch:{ all -> 0x0241 }
        r2.m5250init(r3);	 Catch:{ all -> 0x0241 }
        r1.add(r2);	 Catch:{ all -> 0x0241 }
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r2 = new c.c.a.o;	 Catch:{ all -> 0x0241 }
        r3 = r13.b;	 Catch:{ all -> 0x0241 }
        r2.m5253init(r3);	 Catch:{ all -> 0x0241 }
        r1.add(r2);	 Catch:{ all -> 0x0241 }
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r2 = new c.c.a.p;	 Catch:{ all -> 0x0241 }
        r3 = r13.b;	 Catch:{ all -> 0x0241 }
        r4 = r13.c;	 Catch:{ all -> 0x0241 }
        r2.m5255init(r3, r4);	 Catch:{ all -> 0x0241 }
        r1.add(r2);	 Catch:{ all -> 0x0241 }
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r2 = new c.c.a.z1;	 Catch:{ all -> 0x0241 }
        r3 = r13.b;	 Catch:{ all -> 0x0241 }
        r4 = r13.c;	 Catch:{ all -> 0x0241 }
        r2.m5279init(r3, r4);	 Catch:{ all -> 0x0241 }
        r1.add(r2);	 Catch:{ all -> 0x0241 }
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r2 = new c.c.a.i;	 Catch:{ all -> 0x0241 }
        r3 = r13.b;	 Catch:{ all -> 0x0241 }
        r4 = r13.c;	 Catch:{ all -> 0x0241 }
        r2.m5240init(r3, r4);	 Catch:{ all -> 0x0241 }
        r1.add(r2);	 Catch:{ all -> 0x0241 }
        r1 = r13.e;	 Catch:{ all -> 0x0241 }
        r2 = new c.c.a.d2;	 Catch:{ all -> 0x0241 }
        r3 = r13.c;	 Catch:{ all -> 0x0241 }
        r2.m5224init(r3);	 Catch:{ all -> 0x0241 }
        r1.add(r2);	 Catch:{ all -> 0x0241 }
    L_0x00df:
        monitor-exit(r0);	 Catch:{ all -> 0x0241 }
        r0 = r13.d;
        r1 = new org.json.JSONObject;
        r1.<init>();
        c.c.a.n0.d(r1, r0);
        r0 = r13.e;
        r0 = r0.iterator();
        r2 = 0;
        r3 = 1;
        r4 = 1;
        r5 = 0;
        r6 = 0;
    L_0x00f5:
        r7 = r0.hasNext();
        r8 = 10;
        r9 = 0;
        if (r7 == 0) goto L_0x0187;
    L_0x00fe:
        r7 = r0.next();
        r7 = (c.c.a.b2) r7;
        r10 = r7.a;
        if (r10 == 0) goto L_0x013d;
    L_0x0108:
        r10 = r7.c;
        if (r10 != 0) goto L_0x013d;
    L_0x010c:
        r10 = r13.c;
        r10 = r10.b();
        if (r10 != 0) goto L_0x011a;
    L_0x0114:
        r10 = r7.d;
        if (r10 == 0) goto L_0x011a;
    L_0x0118:
        r10 = 1;
        goto L_0x011b;
    L_0x011a:
        r10 = 0;
    L_0x011b:
        r11 = c.c.a.m0.b;
        if (r11 == 0) goto L_0x013b;
    L_0x011f:
        r11 = new java.lang.StringBuilder;
        r11.<init>();
        r12 = "needSyncFromSub ";
        r11.append(r12);
        r11.append(r7);
        r12 = " ";
        r11.append(r12);
        r11.append(r10);
        r11 = r11.toString();
        c.c.a.m0.a(r11, r9);
    L_0x013b:
        if (r10 == 0) goto L_0x0178;
    L_0x013d:
        r9 = r7.b(r1);	 Catch:{ JSONException -> 0x016a, SecurityException -> 0x0144 }
        r7.a = r9;	 Catch:{ JSONException -> 0x016a, SecurityException -> 0x0144 }
        goto L_0x016e;
    L_0x0144:
        r9 = move-exception;
        r10 = r7.b;
        if (r10 != 0) goto L_0x016e;
    L_0x0149:
        r5 = r5 + 1;
        r10 = "loadHeader, ";
        r10 = c.b.a.a.a.g(r10);
        r11 = r13.g;
        r10.append(r11);
        r10 = r10.toString();
        r11 = "TeaLog";
        android.util.Log.w(r11, r10, r9);
        r9 = r7.a;
        if (r9 != 0) goto L_0x016e;
    L_0x0163:
        r9 = r13.g;
        if (r9 <= r8) goto L_0x016e;
    L_0x0167:
        r7.a = r3;
        goto L_0x016e;
    L_0x016a:
        r8 = move-exception;
        c.c.a.m0.b(r8);
    L_0x016e:
        r8 = r7.a;
        if (r8 != 0) goto L_0x0178;
    L_0x0172:
        r8 = r7.b;
        if (r8 != 0) goto L_0x0178;
    L_0x0176:
        r6 = r6 + 1;
    L_0x0178:
        r8 = r7.a;
        if (r8 != 0) goto L_0x0183;
    L_0x017c:
        r7 = r7.b;
        if (r7 == 0) goto L_0x0181;
    L_0x0180:
        goto L_0x0183;
    L_0x0181:
        r7 = 0;
        goto L_0x0184;
    L_0x0183:
        r7 = 1;
    L_0x0184:
        r4 = r4 & r7;
        goto L_0x00f5;
    L_0x0187:
        if (r4 == 0) goto L_0x01b0;
    L_0x0189:
        r0 = h;
        r7 = r0.length;
    L_0x018c:
        if (r2 >= r7) goto L_0x019d;
    L_0x018e:
        r10 = r0[r2];
        r10 = r1.optString(r10);
        r10 = android.text.TextUtils.isEmpty(r10);
        r10 = r10 ^ r3;
        r4 = r4 & r10;
        r2 = r2 + 1;
        goto L_0x018c;
    L_0x019d:
        r0 = "user_unique_id";
        r0 = r1.optString(r0, r9);
        r2 = android.text.TextUtils.isEmpty(r0);
        if (r2 != 0) goto L_0x01b0;
    L_0x01a9:
        r2 = "user_unique_id";
        r1.put(r2, r0);	 Catch:{ JSONException -> 0x01af }
        goto L_0x01b0;
    L_0x01b0:
        r13.d = r1;
        r13.a = r4;
        r0 = c.c.a.m0.b;
        if (r0 == 0) goto L_0x01e3;
    L_0x01b8:
        r0 = "loadHeader, ";
        r0 = c.b.a.a.a.g(r0);
        r1 = r13.a;
        r0.append(r1);
        r1 = ", ";
        r0.append(r1);
        r1 = r13.g;
        r0.append(r1);
        r1 = ", ";
        r0.append(r1);
        r1 = r13.d;
        r1 = r1.toString();
        r0.append(r1);
        r0 = r0.toString();
        c.c.a.m0.a(r0, r9);
        goto L_0x0201;
    L_0x01e3:
        r0 = "loadHeader, ";
        r0 = c.b.a.a.a.g(r0);
        r1 = r13.a;
        r0.append(r1);
        r1 = ", ";
        r0.append(r1);
        r1 = r13.g;
        r0.append(r1);
        r0 = r0.toString();
        r1 = "TeaLog";
        android.util.Log.i(r1, r0, r9);
    L_0x0201:
        if (r5 <= 0) goto L_0x0215;
    L_0x0203:
        if (r5 != r6) goto L_0x0215;
    L_0x0205:
        r0 = r13.g;
        r0 = r0 + r3;
        r13.g = r0;
        r0 = r13.j();
        if (r0 == 0) goto L_0x0215;
    L_0x0210:
        r0 = r13.g;
        r0 = r0 + r8;
        r13.g = r0;
    L_0x0215:
        r0 = r13.a;
        if (r0 == 0) goto L_0x023e;
    L_0x0219:
        r0 = com.bytedance.embedapplog.AppLog.getDataObserver();
        r1 = r13.d;
        r2 = "device_id";
        r3 = "";
        r1 = r1.optString(r2, r3);
        r2 = r13.d;
        r3 = "install_id";
        r4 = "";
        r2 = r2.optString(r3, r4);
        r3 = r13.d;
        r4 = "ssid";
        r5 = "";
        r3 = r3.optString(r4, r5);
        r0.onIdLoaded(r1, r2, r3);
    L_0x023e:
        r0 = r13.a;
        return r0;
    L_0x0241:
        r1 = move-exception;
        monitor-exit(r0);	 Catch:{ all -> 0x0241 }
        throw r1;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.c.a.c.h():boolean");
    }

    public int j() {
        String str = "";
        String optString = this.d.optString("device_id", str);
        this.d.optString("install_id", str);
        if (!i(optString)) {
            return 0;
        }
        str = "version_code";
        return this.f.getInt(str, 0) == this.d.optInt(str, -1) ? 1 : 2;
    }
}
